﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using HTB.DevFx;
using HTB.DevFx.Core;
using Maple.Weixin.MP.Entities.CustomerService;

namespace Maple.Weixin.MP.Web.Controllers
{
    public class DkfController : Controller
    {
        private readonly IDkfService dkfService = ObjectService.GetObject<IDkfService>();

        //
        // GET: /Dkf/

        public JsonResult Index()
        {
            DkfAccount[] accounts = dkfService.GetAccounts();
            return Json(accounts, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public JsonResult DkfOnline()
        {
            DkfOnline[] accounts = dkfService.GetOnlineAccount();
            return Json(accounts, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DkfAdd(bool isReturnAccount)
        {
            IAOPResult ioResult = dkfService.AddAccount("kf005@testkf001", "kf004", GetMD5("111111"), isReturnAccount);

            return Json(ioResult, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DkfEdit(bool isReturnAccount)
        {
            IAOPResult ioResult = dkfService.EditAccount("kf004@testkf001", "kf004", GetMD5("123456"), isReturnAccount);

            return Json(ioResult, JsonRequestBehavior.AllowGet);
        }


        public JsonResult DkfDel()
        {
            IAOPResult ioResult = dkfService.DeleteAccount("kf004@testkf001");

            return Json(ioResult, JsonRequestBehavior.AllowGet);
        }

        public string GetMD5(string msg)
        {
            StringBuilder sb = new StringBuilder();

            using (MD5 md5 = MD5.Create())
            {
                byte[] buffer = Encoding.UTF8.GetBytes(msg);
                byte[] newB = md5.ComputeHash(buffer);

                foreach (byte item in newB)
                {
                    sb.Append(item.ToString("x2"));
                }
            }

            return sb.ToString();
        } 
    }
}
